//
// Created by Luis on 2022/4/14.
//

#ifndef FFMPEG_LYAUDIO_H
#define FFMPEG_LYAUDIO_H

extern "C"{
#include "libavcodec/avcodec.h"
#include <libswresample/swresample.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
};

#include "LYPlaystatus.h"
#include "LYQueue.h"
#include "LYCallJava.h"

class LYAudio {
public:
    int streamIndex = -1;
    AVCodecParameters *codecpar = NULL;
    AVCodecContext *avCodecContext = NULL;
    LYQueue *queue = NULL;
    LYPlaystatus *playstatus = NULL;
    int sample_rate = 0;
    uint8_t *buffer = NULL;
    pthread_t thread_play;
    int ret = 0;
    int data_size = 0;
    AVPacket *avPacket = NULL;
    AVFrame *avFrame = NULL;

    // 引擎接口
    SLObjectItf engineObject = NULL;
    SLEngineItf engineEngine = NULL;

    //混音器
    SLObjectItf outputMixObject = NULL;
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
    SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

    //pcm
    SLObjectItf pcmPlayerObject = NULL;
    SLPlayItf pcmPlayerPlay = NULL;
    SLVolumeItf pcmVolumePlay = NULL;
    //缓冲器队列
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = NULL;

    double last_time;
    int duration = 0;
    AVRational time_base;
    double now_time;
    double clock;
    LYCallJava *callJava = NULL;

    int mute = 2;
    SLMuteSoloItf  pcmMutePlay = NULL;
    int volumePercent = 100;
public:
    LYAudio(LYPlaystatus *playstatus, int sample_rate,LYCallJava *callJava);
    ~LYAudio();
    void play();
    void pause();
    void initOpenSLES();
    int getCurrentSampleRateForOpensles(int sample_rate);
    int resampleAudio();
    void resume();
    void setMute(jint mute);
    void setVolume(int percent);
};
#endif //FFMPEG_LYAUDIO_H
